package cn.edu.bs.web.controller;

import cn.edu.bs.domain.Borrow;
import cn.edu.bs.domain.User;
import cn.edu.bs.domain.custom.BorrowCustom;
import cn.edu.bs.service.BorrowService;
import cn.edu.bs.service.UserService;
import cn.edu.bs.web.response.APIResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author bs
 */
@Controller
@RequestMapping("/borrows")
public class BorrowController extends BaseController {

    private final String BORROW_LIST_PAGE = "borrow/borrow-list";

    private final BorrowService borrowService;

    private final UserService userService;

    @Autowired
    public BorrowController(BorrowService borrowService, UserService userService) {
        this.borrowService = borrowService;
        this.userService = userService;
    }

    @PostMapping("")
    @ResponseBody
    public APIResult<String> borrowBook(
            @RequestParam(name = "bookId") Integer bookId,
            HttpSession session
    ) {
        try {
            String username = (String) session.getAttribute("username");
            User user = userService.selectByUsername(username);
            Borrow borrow = new Borrow();
            borrow.setBookId(bookId);
            borrow.setBorrowTime(new Date());
            borrow.setUserId(user.getUserId());
            borrowService.insertSelective(borrow);
            return asSuccess("success");
        } catch (Exception e) {
            e.printStackTrace();
            return asError("系统异常，请检查");
        }

    }

    @PostMapping("/return")
    @ResponseBody
    public APIResult<String> returnBook(
            @RequestParam(name = "borrowId") Integer borrowId
    ) {
        try {
            Borrow borrow = borrowService.selectByPrimaryKey(borrowId);
            if (borrow == null) {
                return asError("该图书未被借阅");
            }
            borrow.setIsReturn(true);
            borrow.setIsUrged(true);
            borrowService.updateByPrimaryKeySelective(borrow);
            return asSuccess("success");
        } catch (Exception e) {
            e.printStackTrace();
            return asError("系统异常，请检查");
        }
    }
    @PostMapping("/urged")
    @ResponseBody
    public APIResult<String> urgedBook(
            @RequestParam(name = "borrowId") Integer borrowId
    ) {
        try {
            Borrow borrow = borrowService.selectByPrimaryKey(borrowId);
            if (borrow == null) {
                return asError("该图书未被借阅");
            }
            borrow.setIsUrged(false);
            borrowService.updateByPrimaryKeySelective(borrow);
            return asSuccess("admin/admin-index");
        } catch (Exception e) {
            e.printStackTrace();
            return asError("系统异常，请检查");
        }
    }

    @RequestMapping("")
    public String borrowList(
            HttpSession session,
            Model model
    ) {
        String username = null;
        if (session.getAttribute("username") != null) {
            username = (String) session.getAttribute("username");
            User user = userService.selectByUsername(username);
            List<BorrowCustom> borrows = borrowService.selectByUserId(user.getUserId());
            model.addAttribute("borrows", borrowService.transformCustomsToTOs(borrows));
        }
        return BORROW_LIST_PAGE;
    }
}
